package com.cw.config;

import io.jsonwebtoken.*;
import java.util.*;
import lombok.*;
import org.springframework.stereotype.*;

/**
 * @Author cc
 * @Date 2020/6/28 17:39
 * @Version 1.0 JWT配置
 */
@Component
@Data
public class JwtConfig {

    private String secret = "iwqjhda8232bjgh432[cicada-smile]";
    //12小时过期(毫秒)
    private long expire = 43200000;
    private String header = "token";

    /**
     * 根据身份ID标识，生成Token
     */
    public String getToken(Map<String, Object> map) {
	  Date nowDate = new Date();
	  //过期时间
	  Date expireDate = new Date(nowDate.getTime() + expire);
	  return Jwts.builder().setHeaderParam("typ", "JWT").setClaims(map).setIssuedAt(nowDate)
			 .setExpiration(expireDate).signWith(SignatureAlgorithm.HS512, secret).compact();
    }

    /**
     * 获取 Token 中注册信息
     */
    public Map<String, Object> getTokenClaim(String token) {
	  try {
		return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
	  } catch (Exception e) {
		e.printStackTrace();
		return null;
	  }
    }

    /**
     * 获取 Token 中注册信息
     */
    public Map<String, Object> getTokenClaim(String token,String appId) {
        try {
            return Jwts.parser().setSigningKey(appId).parseClaimsJws(token).getBody();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Token 是否过期验证
     */
    public boolean isTokenExpired(Date expirationTime) {
	  return expirationTime.before(new Date());
    }

}
